package com.general.template.controller;

import com.general.template.enums.ResultEnum;
import com.general.template.export.entity.ImportParams;
import com.general.template.export.handler.ExcelImportHandler;
import com.general.template.pojo.PowerUser;
import com.general.template.service.PowerUserService;
import com.general.template.utils.Pages;
import com.general.template.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 杰哥
 * Date: 2021/2/15 16:54
 * @Description:
 **/
@RestController
@RequestMapping("userManager")
@Api(value="用户后台管理",description = "对用户账号信息的管理")
public class UserManagerController {

    @Autowired
    private PowerUserService powerUserService;

    /**
     * excel文件导入用户
     *
     * @author 杰哥
     * @date 2021/2/16 14:47
     * @param file
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "importAdd",method = RequestMethod.POST)
    @ApiOperation(value="导入excel文件",notes = "批量导入用户信息")
    @ApiImplicitParam(name = "file",value = "上传文件，",paramType = "formData",required = true,dataType = "file")
    public Result<Object> importAdd(MultipartFile file){
        List<PowerUser> list = null;
        InputStream inputStream = null;
        Map<String, Long> map = null;
        ExcelImportHandler importHandler = new ExcelImportHandler();
        try {
            inputStream = file.getInputStream();
            list = importHandler.importExcel(inputStream, PowerUser.class, new ImportParams(0,6));
            System.out.println(list);
            map = powerUserService.addList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<>(ResultEnum.SUCCESS,map);
    }

    /**
     * 添加单个用户
     *
     * @author 杰哥
     * @date 2021/2/16 14:49
     * @param user
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "addUser",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="添加单个用户",notes = "添加单个用户信息")
    public Result<Object> addUser(@RequestBody PowerUser user){
        int addUser = this.powerUserService.addUser(user);
        if (addUser==0){
            return new Result<>(ResultEnum.SUCCESS,"添加失败!该用户已存在");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"添加成功!");
        }
    }

    /**
     * 修改用户信息
     *
     * @author 杰哥
     * @date 2021/2/16 14:50
     * @param user
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "updateInfo",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改用户",notes = "修改用户信息")
    public Result<Object> updateInfo(@RequestBody PowerUser user){
        int updateUser = this.powerUserService.updateUser(user);
        if (updateUser==0){
            return new Result<>(ResultEnum.SUCCESS,"修改失败!该用户不存在");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"修改成功!");
        }
    }

    /**
     * 禁用用户
     *
     * @author 杰哥
     * @date 2021/2/16 14:50
     * @param id
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="禁用用户",notes = "禁用用户登陆")
    public Result<Object> delete(@PathVariable("id") Integer id){
        int updateUser = this.powerUserService.updateStatus(id,1);
        if (updateUser==0){
            return new Result<>(ResultEnum.SUCCESS,"禁用失败!该用户不存在");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"禁用成功!");
        }
    }

    /**
     * 启用用户
     *
     * @author 杰哥
     * @date 2021/2/16 18:02
     * @param id
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "enable/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="启用用户",notes = "启用用户登陆")
    public Result<Object> enable(@PathVariable("id") Integer id){
        int updateUser = this.powerUserService.updateStatus(id,0);
        if (updateUser==0){
            return new Result<>(ResultEnum.SUCCESS,"启用失败!该用户不存在");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"启用成功!");
        }
    }

    /**
     * 分页查询
     *
     * @author 杰哥
     * @date 2021/2/16 15:24
     * @param pages
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="分页查询",notes = "分页查询用户")
    public Result<Object> page(@RequestBody Pages<PowerUser> pages){
        Pages<PowerUser> userPages = this.powerUserService.pageList(pages);
        return new Result<>(ResultEnum.SUCCESS,userPages);
    }

}
